<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="${ctx}/lib/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="${ctx}/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">书籍名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="bookName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">书籍类型</label>
                            <div class="layui-input-inline">
                                <input type="text" name="bookType" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="button" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script src="${ctx}/lib/layui/layui.js" charset="utf-8"></script>
<script>
    var ctx = "${ctx}";
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        table.render({
            elem: '#currentTableId',
            url: ctx + '/bookcase/list',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type:'checkbox', fixed:'center'}
                ,{field: 'id', title: '馆藏ID',  sort: true, fixed: 'left'}
                ,{field: 'bookName', title: '书籍名', align:'center'}
                ,{field: 'bookCount', title: '图书数量', align:'center'}
                ,{field: 'bookType', title: '图书类型', align:'center'}
                ,{field: 'depositArea', title: '图书存放区域', align:'center'}
                ,{field: 'location', title: '图书存放位置', align:'center'}
                ,{title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line'
        });


        //搜索
        form.on('submit(data-search-btn)', function (data) {
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    bookName: $("[name='bookName']").val(),
                    bookType: $("[name='bookType']").val()
                }
            }, 'data');

            return false;
        });

        function deletebook(id) {
            // 询问用户是否确认删除
            layer.confirm('您确定要删除选中的记录吗？',{icon:3, title:'用户管理'}, function (index) {
                // 关闭确认框
                layer.close(index);
                // 发送ajax请求，执行删除用户
                $.ajax({
                    type:"delete",
                    url:ctx + "/bookcase/deleteBook?id="+id,
                    success:function (result) {
                        // 判断删除结果
                        if (result.code === 200) {
                            // 提示成功
                            layer.msg("删除成功！",{icon:6});
                            // 刷新表格
                            table.reload('currentTableId');
                        } else {
                            // 提示失败
                            layer.msg(result.msg, {icon:5});
                        }
                    }
                });
            });
        }

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (data) {
            if (data.event === 'add') {
                // 监听添加操作
                //打开添加/修改用户的对话框
                openAddOrUpdateBookDialog();
            } else if (data.event === 'delete') {
                // 监听删除操作
                console.log(data)
                deletebook(data.data.id);
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (data) {
            console.log(data)
        });

        table.on('tool(currentTableFilter)', function (data) {
            console.log(data.data.id);
            if (data.event === 'edit') {
                //打开添加/修改用户的对话框
                openAddOrUpdateBookDialog(data.data.id);
            } else if (data.event === 'delete') {
                //删除用户
                //获取被选中的数据的信息
                var checkStatus = table.checkStatus(data.config.id);
                console.log(checkStatus);
                //删除多个记录
                deletebook(data.data.id);

            }
        });
        /**
         * 删除多条用户记录
         * */
        function deletebooks(bookData){
            //判断用户是否选择了要删除的记录
            if(bookData.length === 0){
                layer.msg("请选择要删除的记录！",{icon:5});
                return;
            }
            // 询问用户是否确认删除
            layer.confirm('您确定要删除选中的记录吗？',{icon:3, title:'用户管理'}, function (index) {
                // 关闭确认框
                layer.close(index);
                // 传递的参数是数组   ids=1&ids=2&ids=3
                let ids = "bookIds=";
                // 循环选中的行记录的数据
                for(var i = 0; i < bookData.length; i++) {
                    if(i < bookData.length -1) {
                        ids = ids + bookData[i].id + "&bookIds="
                    } else {
                        ids = ids + bookData[i].id;
                    }
                }
                // console.log(ids);

                // 发送ajax请求，执行删除用户
                $.ajax({
                    type:"post",
                    url:ctx + "/bookcase/delete"+ids,
                    //
                    //data:ids, // 传递的参数是数组 ids=1&ids=2&ids=3
                    success:function (result) {
                        // 判断删除结果
                        if (result.code === 200) {
                            // 提示成功
                            layer.msg("删除成功！",{icon:6});
                            // 刷新表格
                            tableIns.reload();
                        } else {
                            // 提示失败
                            layer.msg(result.msg, {icon:5});
                        }
                    }
                });
            });
        }
        /**
         * 打开添加/修改用户的对话框
         * */
        function openAddOrUpdateBookDialog(id){
            var title = "<h3>书籍管理 - 添加书籍</h3>";
            var url = ctx + "/bookcase/toAddOrUpdateBookPage";
            //判断Id是否为空，如果为空，则表示添加操作，否则是修改操作
            if(id != null && id !== ''){
                title = "<h3>书籍管理 - 更新书籍</h3>";
                url += "?id="+id;//传递主键，查询数据
            }
            // iframe层
            layui.layer.open({
                // 类型
                type: 2,
                // 标题
                title: title,
                // 宽高
                area: ['650px', '450px'],
                // url地址
                content: url,
                // 可以最大化与最小化
                maxmin:true
            });
        }

    });
</script>

</body>
</html>